﻿using UnityEngine;
using System.Collections;

public class CollisionManager : MonoBehaviour {

	public Node lastNode;
    void OnTriggerEnter2D(Collider2D collider) 
    {
        if (collider.tag == "Arrow")
        {
			if(collider.GetComponent<ArrowBehaviour>().canGet == true)
			{
	            gameObject.SendMessage("ArrowRecovered", SendMessageOptions.RequireReceiver);
	            Destroy(collider.gameObject);
			}
        }

		if(collider.name == "Find")
		{
			collider.gameObject.transform.parent.gameObject.SendMessage("FoundEnemy",SendMessageOptions.DontRequireReceiver);
		}

		if(collider.tag == "Node")
		{
			if(collider.GetComponent<Node>() != lastNode)
			{
				lastNode = collider.GetComponent<Node>();
				GameObject[] allGameObject = GameObject.FindGameObjectsWithTag("Enemy");
				
				foreach(GameObject go in allGameObject)
				{
					try
					{
						if(go.GetComponent<Enemy>().foundEnemy == true)
							go.SendMessage("recalculePath",SendMessageOptions.DontRequireReceiver);
					}
					catch(System.Exception e)
					{}
				}
			}
		}
	}
}
